<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\logic\admin;
use app\common\model\LessonModel;
use app\common\model\AppUserModel;
use app\common\model\MytaskModel;
use app\common\model\SchoolModel;
/**
 * 学生参加课程记录表
 */
class Lesson
{
	private $grad_array = ['','一年级','二年级','三年级','四年级','五年级','六年级','七年级','八年级','九年级'];
		
	private	$classes_array = ['',
							'一班','二班','三班','四班','五班','六班','七班','八班','九班','十班',
							'十一班','十二班','十三班','十四班','十五班','十六班','十七班','十八班',
							'十九班','二十班','二十一班','二十二班','二十三班','二十四班','二十五班',
							'二十六班','二十七班','二十八班','二十九班','三十班','三十一班','三十二班',
							'三十三班','三十四班','三十五班','三十六班','三十七班','三十八班','三十九班',
							'四十班','四十一班','四十二班','四十三班','四十四班','四十五班','四十六班',
							'四十七班','四十八班','四十九班','五十班'
						];
	public function index($param='',$page=20){

		$data = LessonModel::where('name_id','<>',0)
						->where(function($query) use($param){
							if(!empty($param['start_time'])){
								$query->where('day','>=',$param['start_time']);
							}
							if(!empty($param['end_time'])){
								$query->where('day','<',$param['end_time']);
							}
						})
						->field('day')
						->group('day')
						->order('day desc')
						->paginate($page,false,['query'=>$param])
						->each(function($value,$key){
							//各个学校
							$value['school'] = LessonModel::where('name_id','<>',0)->where('day',$value['day'])->field('school_id')->group('school_id')->select();
							$value['school_count'] = count($value['school']);
							//各个班级
							$datas = [];
							$class_count = 0;
							foreach ($value['school'] as $k => $val) {
								$rs1 = LessonModel::where('name_id','<>',0)->where('day',$value['day'])->where('school_id',$val['school_id'])->field('grade,class,school_id')->group('grade,class,school_id')->select()->toArray();
								if(!empty($rs1)){
									$datas[$k] = $rs1;
									$class_count += count($datas[$k]);
								}

							}
							$value['class'] = $datas;
							$value['class_count'] = $class_count;
							
							//各个学生
							$data_user = [];
							$data_count = 0;
							foreach ($datas as $k => $val) {
					
								foreach ($val as $k1 => $val1) {
									$grad = $this->grad_array[$val1['grade']];
									$class = $this->classes_array[$val1['class']];
							
									$rs2 = AppUserModel::where('schoolId',$val1['school_id'])->where('grad',$grad)->where('classes',$class)->where('role',1)->field('id')->select()->toArray();
									if(!empty($rs2)){
										$data_user[$k1] = $rs2;
										$data_count += count($data_user[$k1]);
									}
								}
							}
							$value['user'] = $data_user;
							$value['user_count'] = $data_count;
							//各个模板和浏览数
							$data_task = [];
							$count_task = 0;
							$count_look = 0;
							foreach($data_user as $k => $val){
								foreach ($val as $k1 => $val1) {
									$rs3 = MytaskModel::where('uid',$val1['id'])->where('type',2)->field('id,look_num')->order('id desc')->find();

									if(!empty($rs3)){
										$data_task[$k1] = $rs3;
						
										$count_task += count($data_task[$k1]);
										$count_look += $rs3['look_num'];
									}
								}
							}
							$value['task'] = $data_task;
							$value['count_task'] = $count_task;
							$value['count_look'] = $count_look;
						});
		return $data;
	}

	public function getList($param='',$page=20){
		if(!empty($param['school_name'])){
			$da = SchoolModel::where('name','like','%'.trim($param['school_name']).'%')->field('id')->select()->toArray();
			$school_ids = 0;
			if(!empty($da)){
				$ar = [];
				foreach ($da as $key => $value) {
					$ar[] = $value['id'];
				}
				$school_ids = implode(',', $ar);
			}
			$param['school_ids'] = $school_ids;
		}

		$data = LessonModel::where('name_id','<>',0)
						->where(function($query) use($param){
							if(!empty($param['start_time'])){
								$query->where('day','>=',$param['start_time']);
							}
							if(!empty($param['end_time'])){
								$query->where('day','<',$param['end_time']);
							}
							if(isset($param['school_ids'])){
								$query->where('school_id','in',$param['school_ids']);
							}
						})
						->group('day,school_id')
						->order('lesson_id desc')
						->paginate($page,false,['query'=>$param])
						->each(function($value,$key){
				//各个班级
				$value['class'] = LessonModel::where('name_id','<>',0)->where('day',$value['day'])->where('school_id',$value['school_id'])->field('grade,class,school_id')->group('grade,class,school_id')->select()->toArray();
				$value['class_count'] = count($value['class']);
				//各个学生
				$data_user = [];
				$class_count = 0;
				foreach ($value['class'] as $k => $val) {
					$grad = $this->grad_array[$val['grade']];
					$class = $this->classes_array[$val['class']];
			
					$rs = AppUserModel::where('schoolId',$val['school_id'])->where('grad',$grad)->where('classes',$class)->where('role',1)->field('id')->select()->toArray();
					if(!empty($rs)){
						$data_user[$k] = $rs;
						$class_count += count($data_user[$k]);
					}
				}
				$value['user'] = $data_user;
				$value['user_count'] = $class_count;


				//各个模板和浏览数
				$data_task = [];
				$count_task = 0;
				$count_look = 0;
				foreach($data_user as $k => $val){
					foreach ($val as $k1 => $val1) {
						$rs3 = MytaskModel::where('uid',$val1['id'])->where('type',2)->field('id,look_num')->order('id desc')->find();

						if(!empty($rs3)){
							$data_task[$k1] = $rs3;
			
							$count_task += count($data_task[$k1]);
							$count_look += $rs3['look_num'];
						}
					}
				}
				$value['task'] = $data_task;
				$value['count_task'] = $count_task;
				$value['count_look'] = $count_look;

				
		});
		return $data;
	}
	
}